package sist.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sun.org.apache.xpath.internal.axes.HasPositionalPredChecker;

import sist.bean.BbsBean;
import sist.dao.Dao;

@Controller
@Component

public class SpringCRUD {
@Resource(name="ibitsDao")
	private Dao dao;


@RequestMapping(value="/jsp/boardInsert.do")
public ModelAndView insertSpringBoard(@ModelAttribute("bean") BbsBean bean,@RequestParam("file")MultipartFile file){
		ModelAndView mv=new ModelAndView();
		String path="C:\\eclipse-jee-kepler-SR2-win32-x86_64\\eclipse_spring\\workspace\\spring\\springWeb\\WebContent\\upload\\";
		try {
			FileOutputStream fos=new FileOutputStream(path+file.getOriginalFilename());
			byte[]fileData=file.getBytes();
			fos.write(fileData);
			bean.setFileName(file.getOriginalFilename());
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	mv.setViewName("redirect:/jsp/springList.do");
	System.out.println(bean);
	dao.insertSpring(bean);
	
	return mv;
}

@RequestMapping(value="/jsp/hit.do")
public String updateHit(BbsBean bean,HttpSession session)throws Throwable{
	 session.setAttribute("infosession", bean);
	 dao.updateHit(bean.getNo());
	 System.out.println(bean+"..hit..");
	 return "redirect:/jsp/info.do";
}

@RequestMapping(value="/jsp/info.do")
public ModelAndView selectInfo(HttpSession session){
	ModelAndView mv=new ModelAndView();
	BbsBean bean=(BbsBean)session.getAttribute("infosession");
	mv.addObject("info",dao.selectSpringInfo(bean.getNo()));
	System.out.println(bean+"..info..");
	mv.setViewName("/jsp/info");
	return mv;
}

@RequestMapping(value="/jsp/springList.do")
public ModelAndView selectList(@RequestParam(value="search",required=false)String search,@RequestParam(value="data",required=false)String data)throws Throwable{
	 HashMap<Object, Object>map=new HashMap<Object, Object>();
	 map.put("search", search);
	 map.put("data", data);		 
	 ModelAndView mv=new ModelAndView();
	 mv.setViewName("/jsp/list");
	 mv.addObject("list",dao.selectSpring(map));
	 return mv;
}

@RequestMapping(value="/jsp/download.do")
public ModelAndView downloadProcess(@RequestParam(value="fileName")String fileName)throws Throwable{

	String path="C:\\eclipse-jee-kepler-SR2-win32-x86_64\\eclipse_spring\\workspace\\spring\\springWeb\\WebContent\\upload\\";
	File file=new File(path+fileName);
	
	return new ModelAndView("download", "downloadFile", file);
}

}
